package com.exam.project.business.service.impl;

import java.util.List;

import com.exam.common.utils.DateUtils;
import com.exam.common.utils.SecurityUtils;
import com.exam.project.business.domain.ExamQuestion;
import com.exam.project.business.dto.QuestionDto;
import com.exam.project.business.mapper.ExamQuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.exam.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.exam.project.business.domain.ChoiceOption;
import com.exam.project.business.mapper.QuestionMapper;
import com.exam.project.business.domain.Question;
import com.exam.project.business.service.IQuestionService;

/**
 * 题目Service业务层处理
 *
 * @author qiuwenbing
 * @date 2025-04-03
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 查询题目
     *
     * @param id 题目主键
     * @return 题目
     */
    @Override
    public Question selectQuestionById(Long id) {
        return questionMapper.selectQuestionById(id);
    }

    /**
     * 查询题目列表
     *
     * @param question 题目
     * @return 题目
     */
    @Override
    public List<Question> selectQuestionList(Question question) {
        List<Question> list = questionMapper.selectQuestionList(question);
        if (question.getExamId() != null) {
            list.forEach(item -> {
                ExamQuestion examQuestion = new ExamQuestion();
                examQuestion.setExamId(question.getExamId());
                examQuestion.setQuestionId(item.getId());
                List<ExamQuestion> examQuestions = examQuestionMapper.selectExamQuestionList(examQuestion);
                if (examQuestions.size() > 0) {
                    item.setChoose(true);
                }
            });
        }
        return list;
    }

    @Override
    public List<QuestionDto> selectMyQuestionList(QuestionDto question) {
        question.setUserId(SecurityUtils.getUserId());
        List<QuestionDto> questionDtos = questionMapper.selectMyQuestionList(question);
        questionDtos.forEach(item -> {
            ChoiceOption choiceOption = new ChoiceOption();
            choiceOption.setQuestionId(item.getId());
            List<ChoiceOption> options = questionMapper.selectMyOptions(item.getId());
            List<QuestionDto.Option> list = new ArrayList<>();
            options.forEach(op -> {
                QuestionDto.Option option = new QuestionDto.Option();
                BeanUtils.copyProperties(op, option);
                option.setQuestionId(item.getId());
                list.add(option);
            });
            item.setOptionList(list);
        });
        return questionDtos;
    }

    /**
     * 新增题目
     *
     * @param question 题目
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQuestion(Question question) {
        question.setCreateTime(DateUtils.getNowDate());
        int rows = questionMapper.insertQuestion(question);
        insertChoiceOption(question);
        return rows;
    }

    /**
     * 修改题目
     *
     * @param question 题目
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQuestion(Question question) {
        question.setUpdateTime(DateUtils.getNowDate());
        questionMapper.deleteChoiceOptionByQuestionId(question.getId());
        insertChoiceOption(question);
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除题目
     *
     * @param ids 需要删除的题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionByIds(Long[] ids) {
        questionMapper.deleteChoiceOptionByQuestionIds(ids);
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除题目信息
     *
     * @param id 题目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQuestionById(Long id) {
        questionMapper.deleteChoiceOptionByQuestionId(id);
        return questionMapper.deleteQuestionById(id);
    }

    /**
     * 新增选项信息
     *
     * @param question 题目对象
     */
    public void insertChoiceOption(Question question) {
        List<ChoiceOption> choiceOptionList = question.getChoiceOptionList();
        Long id = question.getId();
        if (StringUtils.isNotNull(choiceOptionList)) {
            List<ChoiceOption> list = new ArrayList<ChoiceOption>();
            for (ChoiceOption choiceOption : choiceOptionList) {
                choiceOption.setQuestionId(id);
                list.add(choiceOption);
            }
            if (list.size() > 0) {
                questionMapper.batchChoiceOption(list);
            }
        }
    }
}
